package com.bestv.utils.database;

import com.bestv.utils.config.GlobalSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 数据库连接器
 * User: Henry
 * Date: 14-5-5
 */
public class DBConnectionManager {
    private static Logger logger = LoggerFactory.getLogger(DBConnectionManager.class);

    // master database connection
    private static String dbDriverMaster = GlobalSettings.getInstance().getDbDriver();
    private static String dbUrlMaster = GlobalSettings.getInstance().getMasterDbUrl();
    private static String dbUserNameMaster = GlobalSettings.getInstance().getDbUserName();
    private static String dbPasswordMaster = GlobalSettings.getInstance().getDbPassword();

    // slave database connection
    private static String dbDriverSlave = GlobalSettings.getInstance().getDbDriver();
    private static String dbUrlSlave = GlobalSettings.getInstance().getDbUrl();
    private static String dbUserNameSlave = GlobalSettings.getInstance().getDbUserName();
    private static String dbPasswordSlave = GlobalSettings.getInstance().getDbPassword();

    public static Connection getMasterConnection() {
        Connection connection = null;
        try {
            connection = JDBCHelper.getConnection(dbDriverMaster, dbUrlMaster, dbUserNameMaster, dbPasswordMaster);
        } catch (SQLException e) {
            logger.error("Error on create master connection.", e);
        }
        return connection;
    }

    public static Connection getSlaveConnection() {
        Connection connection = null;
        try {
            connection = JDBCHelper.getConnection(dbDriverSlave, dbUrlSlave, dbUserNameSlave, dbPasswordSlave);
        } catch (SQLException e) {
            logger.error("Error on create slave connection.", e);
        }
        return connection;
    }
}
